package com.uyayo.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.cms.entity.CmsSite;
import com.uyayo.cms.mapper.CmsSiteMapper;
import com.uyayo.cms.model.CmsSiteCondition;
import com.uyayo.cms.service.CmsSiteService;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;


@Service("cmsSiteService")
public class CmsSiteServiceImpl implements CmsSiteService {


	private final CmsSiteMapper cmsSiteMapper;

	@Autowired
	public CmsSiteServiceImpl(CmsSiteMapper cmsSiteMapper) {
		this.cmsSiteMapper = cmsSiteMapper;
	}


	@Override
	public CmsSite getSiteById(Long id){
		return cmsSiteMapper.selectByPrimaryKey(id);
	}
	

	@Override
	public List<CmsSite> selectSite(CmsSiteCondition cmsSiteCondition) {
		Example example = new Example(CmsSite.class);
		example.createCriteria();
		return cmsSiteMapper.selectByExample(example);
	}
	
	@Override
	public PageBean<CmsSite> selectSiteByPaging(Integer pageCurrent, Integer pageSize, String sort, String order, CmsSiteCondition cmsSiteCondition) {

		List<CmsSite> userList;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("createTime", "create_time"));
		orderBies.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil  orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);
		
		Example example = new Example(CmsSite.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(cmsSiteCondition.getName())){
			criteria.andLike("name","%"+cmsSiteCondition.getName()+"%");
		}
		if(cmsSiteCondition.getBeginCreatedTime()!=null || cmsSiteCondition.getEndCreatedTime()!=null){
			criteria.andBetween("createTime", cmsSiteCondition.getBeginCreatedTime(), cmsSiteCondition.getEndCreatedTime());
		}
		userList = cmsSiteMapper.selectByExample(example);

		//取记录总条数
		PageInfo<CmsSite> pageInfo = new PageInfo<>(userList);
		
		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, userList);
	}
	

	@Override
	public int createCmsSite(CmsSite cmsSite) {
		return cmsSiteMapper.insertSelective(cmsSite);
	}

	@Override
	public CmsSite getSiteByUpdateTestOnly(String code) {
		CmsSite cmsSite = new CmsSite();
		cmsSite.setCode(code);
		return cmsSiteMapper.selectOne(cmsSite);
	}

	@Override
	public CmsSite selectOne(CmsSite cmsSite) {
		return cmsSiteMapper.selectOne(cmsSite);
	}

	@Override
	public int updateSite(CmsSite cmsSite) {
		return cmsSiteMapper.updateByPrimaryKeySelective(cmsSite);
	}

	@Override
	public int deleteSite(Integer id) {
		return cmsSiteMapper.deleteByPrimaryKey(id);
	}
	
}
